package com.easylinkin.linkappapi.quality.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.quality.entity.QualityConfig;
import com.easylinkin.linkappapi.quality.mapper.QualityConfigMapper;
import com.easylinkin.linkappapi.quality.service.QualityConfigService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 通知设置 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class QualityConfigServiceImpl extends ServiceImpl<QualityConfigMapper, QualityConfig> implements QualityConfigService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param qualityConfig
   */
  @Override
  public void insert(QualityConfig qualityConfig) {
    /**
     * 验证重复
     */
    this.checkExist(qualityConfig);
    //设置基本属性
    this.setBase(qualityConfig);
    //设置生效时间
    qualityConfig.setCheckUpperTime(qualityConfig.getCreateTime());
    qualityConfig.setRectifyUpperTime(qualityConfig.getCreateTime());
    this.save(qualityConfig);
  }

  /**
   * 根据id编辑
   * @param qualityConfig
   */
  @Override
  public void updateOne(QualityConfig qualityConfig) {
    /**
     * 验证重复
     */
    this.checkExist(qualityConfig);
    //设置基本属性
    this.setBase(qualityConfig);
    //设置生效时间
    QualityConfig old = baseMapper.selectById(qualityConfig.getId());
    if (null!=qualityConfig.getRectifyUpper() && !qualityConfig.getRectifyUpper().equals(old.getRectifyUpper())){
      qualityConfig.setRectifyUpperTime(qualityConfig.getModifyTime());
    }
    if (null!=qualityConfig.getCheckUpper() && !qualityConfig.getCheckUpper().equals(old.getCheckUpper())){
      qualityConfig.setCheckUpperTime(qualityConfig.getModifyTime());
    }
    this.updateById(qualityConfig);
  }

  /**
   * @Description: 查询通知设置详情
   * @author kan yuanfeng
   * @date 2023/04/04 11:42
   */
  @Override
  public QualityConfig findOne() {
    QualityConfig qualityConfig = new QualityConfig();
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    QueryWrapper<QualityConfig> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId);
    List<QualityConfig> configs = baseMapper.selectList(queryWrapper);
    if (CollectionUtil.isNotEmpty(configs)){
      qualityConfig = configs.get(0);
    }else {
      qualityConfig.setRectify(true);
      qualityConfig.setCheck(true);
      qualityConfig.setCheckEnd(true);
    }
    return qualityConfig;
  }

  /**
   * 验证重复
   */
  private void checkExist(QualityConfig qualityConfig) {
    QueryWrapper<QualityConfig> queryWrapper = new QueryWrapper<>();
    //设置判断重复条件
    queryWrapper.eq("tenant_id_",linkappUserContextProducer.getCurrent().getTenantId());
    //编辑的时候存在id
    Optional.ofNullable(qualityConfig.getId()).ifPresent(id -> queryWrapper.ne("id",qualityConfig.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该通知设置已存在");
    }
  }

  /**
   * 设置基本属性
   * @param qualityConfig
   */
  private void setBase(QualityConfig qualityConfig) {
    qualityConfig.setModifyTime(new Date());
    if(null != linkappUserContextProducer.getCurrent()){
      qualityConfig.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == qualityConfig.getId()){
      if(null != linkappUserContextProducer.getCurrent()){
        qualityConfig.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      }
      qualityConfig.setCreateTime(new Date());
      if(StringUtils.isBlank(qualityConfig.getTenantId())){
        qualityConfig.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
      }
    }
  }
}
